package com.huang.A01_case;

/**
 * 目标
 * 完成数字加密案例
 */
public class Demo4 {
    public static void main(String[] args) {
        System.out.println(encrypt(8346));
    }

    public static String encrypt(int number){
        //拆分数字
        int[] numbers = split(number);
        //遍历数组进行加密
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = (numbers[i] + 5) % 10;
        }
        //数组反转
        reverse(numbers);
        //拼接数字
        String encrypt = "";
        for (int i = 0; i < numbers.length; i++) {
            encrypt += numbers[i];
        }
        return encrypt;
    }

    public static void reverse(int[] numbers) {
        for (int i = 0, j = numbers.length - 1; i < j; i++, j--) {
            //定义临时变量存放后一个位置的值
            int temp = numbers[j];
            //将前一个位置的值赋值给后一个位置
            numbers[j] = numbers[i];
            //将temp中记录的后一个位置的值赋值给前一个位置
            numbers[i] = temp;
        }
    }

    public static int[] split(int number) {
        //定义动态初始化数组存放拆分的数字
        int[] numbers = new int[4];
        numbers[0] = number / 1000;
        numbers[1] = (number / 100) % 10;
        numbers[2] = (number / 10) % 10;
        numbers[3] = number % 10;
        return numbers;
    }
}